<?php

class AuthController extends BaseController
{

    /*
    |--------------------------------------------------------------------------
    | Default Home Controller
    |--------------------------------------------------------------------------
    |
    | You may wish to use controllers instead of, or in addition to, Closure
    | based routes. That's great! Here is an example controller method to
    | get you started. To route to this controller, just add the route:
    |
    |	Route::get('/', 'HomeController@showWelcome');
    |
    */

    public function login()
    {
        return View::make('auth.login');
    }

    public function auth()
    {
        $usernameinput = Input::get('username');
        $password = Input::get('password');
        $field = filter_var($usernameinput, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';

        if (Input::get('remember_me') == 'on') {
            if (Auth::attempt(array($field => $usernameinput, 'password' => $password), true)) {
                return Redirect::intended('/');
            }
        } else {
            if (Auth::attempt(array($field => $usernameinput, 'password' => $password))) {
                return Redirect::intended('/');
            }
        }
        return Redirect::back()->with('error', '用户不存在或密码错误');
    }

    public function logout(){
        Auth::logout();
        return Redirect::route('logout')->with('message', "已经安全退出");
    }
}